home *** CD-ROM | disk | FTP | other *** search
/ Delphi 2 - Developers' Solutions / Delphi 2 Developers' Solutions.iso / dds / sharware / dynarray / readme.txt < prev   
Encoding:
Internet Message Format  |  1996-05-27  |  7.2 KB

  1. From: 
  2.   Peter Below
  3.   Geisenheimer Str. 88
  4.   D60529 Frankfurt am Main
  5.   Germany
  6.  
  7. To:
  8.   Nathan Wallace
  9.   530 Meadowlark Drive
  10.   Lakewood, CO 80226
  11.   U.S.A.
  12.  
  13.  
  14.  
  15.  
  16. Dynamic Arrays in Delphi
  17. ========================
  18.  
  19. This archive should contain the following files:
  20.  
  21. readme.txt    This file
  22. arrays.hlp    Help file describing the array classes
  23. arrays.pas    Unit implementing the array classes
  24. fastmem.pas   Auxillary Unit used by Arrays.PAS, see helpfile
  25. multidim.pas  Bonus Unit with a bare-bones multidimensional 
  26.               array class.
  27. arrtest.dpr   Project file for the test program
  28. arrtest1.dfm  Main form of the test program
  29. arrtest2.dfm  Secondary form
  30. arrtest3.dfm  "
  31. arrtest4.dfm  "
  32. arrtest5.dfm  "
  33. arrtest6.dfm  "
  34. arrtest7.dfm  "
  35. arrtest8.dfm  "
  36. arrtest1.pas  Main Unit of the the test program
  37. arrtest2.pas  Secondary unit
  38. arrtest3.pas  "
  39. arrtest4.pas  "
  40. arrtest5.pas  "
  41. arrtest6.pas  "
  42. arrtest7.pas  "
  43. arrtest8.pas  "
  44.  
  45. Author
  46. ======
  47.  
  48. Dr. Peter Below
  49. e-mail: CIS 100113,1101
  50.         Internet: 100113.1101@compuserve.com    (preferred)
  51.                   below@msmrd.frankfurt.hoechst-ag.d400.de
  52.  
  53. This code is freeware and released to the public domain. You may use
  54. it as you see fit in your own applications and distribute it as part
  55. of an application without royalties. You may also distribute the
  56. source code in this archive, preferrable in form of this archive,
  57. provided that you give due credit to the author, include this section
  58. of the readme file unchanged, and don't charge more for it than is
  59. warranted by the cost of media and distribution per se.
  60.  
  61. I give no warranties as to the proper functioning of the code in this
  62. archive. It has been tested under both Delphi 1.0 and Delphi 2.0 and
  63. i'm not aware of any problems with it. However, that does not
  64. guarantee that none exist, and i will not take responsibilty or
  65. provide compensation for any damage done to software, data, hardware,
  66. personal, lifestock, buildings, and the rest of the pyhsical and
  67. spiritual world as a consequence of errors in this code!
  68.  
  69. Installation
  70. ============
  71.  
  72. The only files you need for using the array classes are arrays.pas
  73. and fastmem.pas, copy these to a directory on your harddisk that is
  74. on the LIB search path you have set in Delphi. You will probably also
  75. want to use the help file, so copy arrays.hlp to a directory of your
  76. choice and create a icon for it in your favourite program manager
  77. group. There's no provision to integrate this helpfile into the
  78. Delphi help system, sorry, you have to open and search it manually
  79. (or use a proper editor like CodeWright, that can extract the
  80. keywords from the helpfile itself <g>).
  81.  
  82. If you want to try the test program, copy the arrtest*.* files to a
  83. directory on the harddisk, open the arrtest.dpr project and compile
  84. it. The forms have been developed on 1024*768 large font video, i
  85. hope they scale correctly on other resolutions. The test program just
  86. shows how to use several of the array classes and features like
  87. resize, sort and clone. It has no associated documentation, just play
  88. around with it.
  89.  
  90. Description
  91. ===========
  92.  
  93. Arrays as implemented in Borland's Pascal dialect (and the Pascal
  94. standard as defined by Wirth) are declared at compile time. If you
  95. need to allocate an array with a size only known at run time the only
  96. option is to use a pointer to a large array type and only allocate as
  97. much memory as needed:
  98.  
  99.  Type
  100.    TDoubleArray = Array [0..High(Word) div Sizeof(Double) - 1] of Double;
  101.    PDoubleArray = ^TDoubleArray;
  102.  
  103.  Var
  104.    pMyArray : PDoubleArray;
  105.    i, numItems : Integer;
  106.  Begin
  107.    numItems := 200;
  108.    GetMem( pMyArray, numItems * Sizeof(Double));
  109.    For i:= 0 To Pred(numItems) Do
  110.      pMyArray^[i] := ....
  111.  
  112. Such a dynamically allocated array can also be resized with
  113. ReAllocMem. The main problem is keeping track of the actual upper
  114. bound of the array. This approach effectively disables range checking
  115. by the compiler since the type declaration for the array is what the
  116. compiler uses to determine the highest valid index in its range
  117. checking code. So you have to pass the actual size around with the
  118. pointer to the array wherever you use it and guard against range
  119. errors yourself. Combined with the issue of allocating and
  120. deallocating memory correctly and the use of pointers (deemed
  121. un-Pascalian by many) this is a source of errors that is hard to
  122. avoid. Writing reusable code with such a construct is not impossible
  123. but harder than it ought to be <g>.
  124.  
  125. The natural approach with OOP available in Delphi is to encapsulate
  126. all this mess into a class and be done with it once and for all. This
  127. is what the ARRAYS.PAS unit in this archive does. 
  128.  
  129. Many of the tasks such an array class has to do are actually
  130. independent of the items it stores. These functionalities are thus
  131. implemented in a base class, TBaseArray. This class has methods to
  132. deal with data items on a rather abstract level. The only things it
  133. needs to know are the size of a data item and how many it has to
  134. store. That is enough to handle allocation and deallcoation of
  135. memory, to provide basic support for sorting, insertion, deletion,
  136. copying, to store and retrieve individual items. The class takes care
  137. of all memory management related items, keeps count of how many items
  138. it can store, does index range checking (if required), can be
  139. resized, cloned, assigned, stored to stream or disk and read from the
  140. same sources. Access to individual items requires method calls,
  141. however, the common array syntax cannot be used here.
  142.  
  143. Specialized arrays for certain data types are derived from
  144. TBaseArray. The Arrays Unit already provides a couple of these types
  145. for the standard data types. These derived classes mainly do two
  146. things: provide the correct sort method for this data type and
  147. provide a Data property which allows access to items using the normal
  148. array syntax.
  149.  
  150. There is a price for all this comfort: speed. Using objects entails a
  151. certain overhead. To access a method or field of an object the
  152. code has to load the address of the object first (indirect
  153. referencing). Accessing an array item via the Data property also
  154. involves a method call. The code has to execute more instructions to
  155. access an item in an array object than to access one in a normal
  156. array. This becomes rather noticeable in code that accesses all items
  157. of an array in sequence, e.g. a loop over all array items to do a
  158. sum. These can take 2-3 times as long with an array object. Using
  159. one of the iterator methods for such a task adds another method call
  160. and thus is even slower than a straight For loop using the Data
  161. property!
  162.  
  163. In cases where speed is important it may pay to store the
  164. Memory-property to a pointer-to-an-array-type variable and then
  165. proceed as in the classical approach cited above.
  166.  
  167. The array classes are limited to 64KBytes in Delphi 1.0 since they
  168. store all items in one contiguous memory block. This limit does not
  169. exist in Delphi 2.0. The array classes are also one-dimensional only.
  170. The sample class in MULTIDIM.PAS shows a possible approach to
  171. building multidimensional array classes on top of the one-dimensional
  172. ones in arrays.pas.
  173.  
  174. Have fun with these units and don't hesitate to contact me if you
  175. have problems or questions.
  176.  
  177. May 1996
  178. Peter Below
  179.